package com.lianbang.pharmacy.adapter

import android.widget.ImageView
import cn.ubest.mvvmcore.utils.GlideUtils
import com.chad.library.adapter.base.BaseNodeAdapter
import com.chad.library.adapter.base.entity.node.BaseNode
import com.chad.library.adapter.base.provider.BaseNodeProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lianbang.pharmacy.R
import com.lianbang.pharmacy.bean.MedicineCategory2ListBean
import com.lianbang.pharmacy.bean.MedicineCategory3ListBean

/**
 * Created by maoqi on 2020/11/29.
 */
class ProductListAdapter : BaseNodeAdapter {

    constructor(nodeList: MutableList<BaseNode>? = null) : super(nodeList) {
        // 需要占满一行的，使用此方法（例如section）
        addFullSpanNodeProvider(HeaderProvider())
        // 普通的item provider
        addNodeProvider(ItemProvider())
    }

    override fun getItemType(data: List<BaseNode>, position: Int): Int {
        val bean = data[position]
        return if (bean is MedicineCategory2ListBean) {
            1
        } else {
            2
        }
    }

    class HeaderProvider : BaseNodeProvider() {
        override val itemViewType: Int
            get() = 1

        override val layoutId: Int
            get() = R.layout.header_product_list

        override fun convert(helper: BaseViewHolder, item: BaseNode) {
            if (item is MedicineCategory2ListBean) {
                helper.setText(R.id.tv_title, item.cate_name)
            }
        }
    }

    class ItemProvider : BaseNodeProvider() {
        override val itemViewType: Int
            get() = 2

        override val layoutId: Int
            get() = R.layout.item_product_list

        override fun convert(helper: BaseViewHolder, item: BaseNode) {
            if (item is MedicineCategory3ListBean) {
                helper.setText(R.id.tv_title, "安神补脑")
                GlideUtils.display(context, item.pic, helper.getView<ImageView>(R.id.iv_img))
            }
        }
    }
}